package com.gzu.emr.controller;

import com.gzu.emr.dto.MedicalRecord;
import com.gzu.emr.service.MedicalRecordService;
import com.gzu.emr.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

import static com.gzu.emr.util.ResultGenerator.genFailResult;
import static com.gzu.emr.util.ResultGenerator.genSuccessResult;

@Controller
@RequestMapping("/medical_record")
public class MedicalRecordController {

    @Resource
    private MedicalRecordService medicalRecordService;


    @PostMapping(value = "/save")
    @ResponseBody
    @Operation(description = "新增病历信息")
    @Parameters({@Parameter(name = "patient_id", description = "患者id", required = true),
            @Parameter(name = "doctor_id", description = "医生id", required = true),
            @Parameter(name = "pri_desc", description = "主述", required = true),
            @Parameter(name = "present_illness", description = "现有病症", required = true),
            @Parameter(name = "pres_ill_cond", description = "现有病症医疗情况", required = true),
            @Parameter(name = "PMH", description = "既往病史", required = true),
            @Parameter(name = "allergy", description = "过敏史", required = true),
            @Parameter(name = "health_checkup", description = "体格检查情况"),
            @Parameter(name = "check_advice", description = "检查建议", required = true),
            @Parameter(name = "attention", description = "注意事项", required = true)})
    public Result addMedicalRecord(@RequestBody Map<String, Object> params) {
        var patientId = params.get("patient_id");
        if(patientId == null) {
            return genFailResult("bad request body");
        }
        var doctorId = params.get("doctor_id");
        if(doctorId == null) {
            return genFailResult("bad request body");
        }
        var priDesc = params.get("pri_desc");
        if(priDesc == null) {
            return genFailResult("bad request body");
        }
        var presentIllness = params.get("present_illness");
        if(presentIllness == null) {
            return genFailResult("bad request body");
        }
        var presIllCond = params.get("pres_ill_cond");
        if(presIllCond == null) {
            return genFailResult("bad request body");
        }
        var PMH = params.get("PMH");
        if(PMH == null) {
            return genFailResult("bad request body");
        }
        var allergy = params.get("allergy");
        if(allergy == null) {
            return genFailResult("bad request body");
        }
        var healthCheckup = params.get("health_checkup");
        if(healthCheckup == null) {
            return genFailResult("bad request body");
        }

        var checkAdvice = params.get("check_advice");
        if(checkAdvice == null) {
            return genFailResult("bad request body");
        }

        var attention = params.get("attention");
        if(attention == null) {
            return genFailResult("bad request body");
        }

        MedicalRecord record = new MedicalRecord();
        record.setPatientId((Integer)patientId);
        record.setDoctorId((Integer) doctorId);
        record.setPriDesc((String) priDesc);
        System.err.println((String) priDesc);
        System.err.println(record.getPriDesc());
        record.setPresentIllness((String) presentIllness);
        record.setMedicalCondition((String) presIllCond);
        record.setPMH((String) PMH);
        record.setAllergy((String) allergy);
        record.setHealthCheckup((String) healthCheckup);
        record.setCheckAdvice((String)checkAdvice);
        record.setAttention((String)attention);
        record.setDate(new Date());

        if (medicalRecordService.addMedicalRecord(record) != 1){
            return genFailResult("server internal error");
        } else {
            return genSuccessResult();
        }
    }


    @PostMapping(value = "/patient_last")
    @ResponseBody
    @Operation(description = "查询患者最近的病历")
    @Parameters({@Parameter(name = "patient_id", description = "患者id", required = true)})
    public Result getLastRecordByPatientId(@RequestBody Map<String, Object> params) {
        var patientId = params.get("patient_id");
        if(patientId == null) {
            return genFailResult("bad request error");
        }
        MedicalRecord record = medicalRecordService.selectLastRecordByPatientId((Integer)patientId);
        return genSuccessResult(record);
    }

    @ResponseBody
    @RequestMapping(value = "/list/all", method = RequestMethod.GET)
    public Result list() {
        var records = medicalRecordService.listRecords();
        return genSuccessResult(records);
    }

    @ResponseBody
    @RequestMapping(value = "/list/patient_id", method = RequestMethod.POST)
    @Parameters(@Parameter(name = "patient_id", description = "患者id"))
    public Result listByPatientId(@RequestBody Map<String, Object> params) {
        var pid = params.get("patient_id");
        var records = medicalRecordService.listRecordsByPatientId((Integer) pid);
        return genSuccessResult(records);
    }

    @ResponseBody
    @RequestMapping("/list/patient_name")
    @Parameters(@Parameter(name = "name", description = "患者姓名"))
    public Result listByPatientName(@RequestBody Map<String, Object> params) {
        var pname = params.get("name");
        var records = medicalRecordService.listRecordsByPatientName((String) pname);
        return genSuccessResult(records);
    }
}
